<?php
/**
 * General functions file
 *
 * This are general functions that are available thrue all the framework.
 *
 * LICENSE: This file is part of Elfin.
 *
 *   Elfin is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Elfin is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Elfin.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Filipe Silva <silvam.filipe@gmail.com>
 * @category Core
 * @license GNU General Public License <http://www.fsf.org/licensing/licenses/gpl.html>
 */

require_once dirname(__FILE__) . '/class_paths.php';

/**
 * PHP5 autoload definition
 *
 * This is the definitoin of PHP5 autoload function. It throws 2 exceptions:
 * @throws ClassFileNotFoundException when a rule is found but the class file isn't in rule's path declarations
 * @throws CannotLoadClassException when no rull is found.
 * @param String $class_name
 * @return null
 */
function __autoload($class_name) {
    $rule = _get_class_paths($class_name);
    if ($rule){
        $filename = _class_to_filename($rule, $class_name);
        foreach ($rule['paths'] as $path) {
            if (file_exists($path . $filename)) {
                require_once $path . $filename;
                return;
            }
        }
        throw new ClassFileNotFoundException($filename, $rule['name']);
    }
    throw new CannotLoadClassException($class_name);
}

/**
 * Converts class to file name
 * Converts class name into a proper file name to be loaded by autoload function
 * @param Array $class The selected rule array
 * @param String $class_name The class name to load
 * @return Strinf The class file name
 */
function _class_to_filename($class, $class_name) {
    //convert all to lowercase
    $fileName = strtolower($class_name);
    //correct perfix
    $fileName = str_replace(strtolower($class['prefix']), strtolower($class['prefix']).'_', $fileName);
    //correct sufix
    $fileName = str_replace(strtolower($class['sufix']), '_'.strtolower($class['sufix']), $fileName);
    return $fileName.'.php';
}

/**
 * Filter the class rule
 *
 * Try to find the rule for a given class name. Rules are defined on $class_paths global
 * @global Array $class_paths The definition array of class load rules
 * @param String $class_name The class name to load
 * @return mixed False if no rulle is found. The rule array when found.
 */
function _get_class_paths($class_name) {
    global  $class_paths;
    foreach ($class_paths as $key => $class) {
        if (preg_match($class['pattern'], $class_name)) {
            $class['name'] = $key;
            return $class;
        }
    }
    return FALSE;
}

/**
 * Print variable
 * Used for debug proposes. It uses the print_r function between HTML PRE tags.
 * @param mixed $var The var that will be printed.
 */
function pr($var) {
    echo '<pre>';
    print_r($var);
    echo '</pre>';
}
?>